package com.iso8583.util;

import com.iso8583.Iso8583Parser;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;
import java.util.Map;

public class J8583Util {
	public static Logger log = LoggerFactory.getLogger(J8583Util.class);
	
	public static final String db_trm_tek = "72EF43EA8579E0704815629DB9B3438C";
	public static String trm_sn = "A001000332065441";
	
	//public static String db_trm_tek = "6A0ABB0C1F60620BAFE5F30D94F11AA9";  //"652CEECB44DD8B4D47D3C6DA7F768E87"
	//public static String trm_sn = "A001000129075751";   //"A001000332065634"
	
	public static String head = "6000010000603100311F31";
	public static final String track_2 = "30338013605D1510D000050100000A26E7B0D";//"955100292b248a20143d00002204669067d4e0";
	public static String app_ver = "1.31";
	public static final String mac = "3939393939393939";
	public static String tpdu = head.substring(0, 10);
	public static String header = head.substring(10, 22);
	public static String posKey = DES.DEC_Tripedes(db_trm_tek,"00000000000000000000000000000000");
	public static String field62;
	public static String batchNo;
	public static String merc_cd;
	public static String trm_no;
	public static byte[] unmacmessage;
	public static String cseqNo;
	public static String ttxnAmt;
	public static String aut_cd;
	public static String ctxn_dt;
	public static String newCseqNo;
	public static String oldCseqNo;
	public static String typeCode;
	public static String mustFields;
	public static String optionalFields;
	public static String preCode;
	public static int costTime;
	public static Map<String,String> body;
	public static Map<String, Object> response;
	//
		public static byte[] getLength(byte[] args){
			int headLen = args.length;
			String headLenstr = Integer.toHexString(headLen);
			headLenstr = "0000" + headLenstr;
			headLenstr = headLenstr.substring(headLenstr.length() - 4);
			byte[] bts = new byte[headLenstr.length() / 2];
			int i = 0;
			for (int j = 0; j < bts.length; ++j) {
				bts[j] = (byte) Integer
						.parseInt(headLenstr.substring(i, i + 2), 16);
				i += 2;
			}
			args= ArrayUtils.addAll(bts, args);
			return args;
		}
		//
		public static byte[] getParseBytes(byte[] msg) throws Exception {
			
		    Socket socket = new Socket("10.10.200.91", 6665);//10.10.200.91
            InputStream input = socket.getInputStream();
            OutputStream out = socket.getOutputStream();
            out.write(msg);//
            out.flush();
            int l = 0;
            byte bs[] = new byte[512];
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while ((l = input.read(bs)) > -1) {
                byte temp[] = Arrays.copyOf(bs, l);
                bos.write(temp);
                
                
                if(bos.toByteArray().length>2){
                    
                    //bos.toByteArray()[0] bos.toByteArray()[1]
                    //Integer.parseInt(HexConvertUtil.byte2HexStr(lengths), 16);
                }
                //System.out.println("收到："+getChars(bos.toByteArray()));
            }
            socket.close();
            byte[] body =bos.toByteArray();//toByteArray()
            byte[] d8583 = new byte[body.length - 13];

            System.arraycopy(body, 13, d8583, 0, d8583.length);
            return d8583;
		}
		
		//
		public static Map<String, String> parse(byte[] bufUnCompress) throws Exception {
			Map<String, String> response = null;
			try {
				response = Iso8583Parser.parse("pos", bufUnCompress,false);
			} catch (Exception e) {
			}
			/*String msg =response.get("RSP_INF").toString();
			if(msg!=null){
				 
			}
			else{
				msg="";
			}*/
			//log.warn("�յ���Ӧ��posӦ����Ϊ[{}]��Ӧ����ϢΪ[{}]",response.get("CPSCOD"),msg);
			return response;
			}
		//
		public static String CreateRand(){
		    String charList="1234567890";
		    char[] rdns= new char[6];
		    for(int i=0;i<6;i++){
			int rand = (int)(Math.random()*10);
			rdns[i]=charList.charAt(rand);
		    }
		    return String.valueOf(rdns);
		}
}
